/*
 * Copyright 2009-2011 Prime Technology.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.component.chart.flow;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.primefaces.component.chart.BaseChartRenderer;
import org.primefaces.component.chart.UIChart;
import org.primefaces.model.chart.flow.FlowChartModel;

public class FlowChartRenderer extends BaseChartRenderer {

	@Override
	public void encodeEnd(FacesContext context, UIComponent component)
			throws IOException {
		FlowChart chart = (FlowChart) component;

		encodeDiv(context, chart);
		encodeScript(context, chart);
	}

	protected void encodeScript(FacesContext context, UIChart uichart)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		FlowChart chart = (FlowChart) uichart;
		String clientId = chart.getClientId(context);

		writer.startElement("script", null);
		writer.writeAttribute("type", "text/javascript", null);

		writer.write("$(function(){");

		writer.write(chart.resolveWidgetVar()
				+ " = new PrimeFaces.widget.FlowChart('" + clientId + "', {");

		encodeOptions(context, chart);

		writer.write("}, ");

		encodeData(context, chart);

		writer.write(");});");

		writer.endElement("script");
	}

	protected void encodeDiv(FacesContext context, FlowChart chart)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		String clientId = chart.getClientId(context);

		writer.startElement("div", null);
		writer.writeAttribute("id", clientId, null);
		writer.writeAttribute("style", chart.getStyle(), null);
		writer.writeAttribute("class", chart.getStyleClass(), null);
		writer.writeAttribute("oncontextmenu", "return false;", null);

		writer.startElement("canvas", null);
		writer.writeAttribute("id", clientId + "_canvas", null);
		writer.writeAttribute("width", chart.getWidth(), null);
		writer.writeAttribute("height", chart.getHeight(), null);
		writer.endElement("canvas");

		writer.endElement("div");
	}

	protected void encodeData(FacesContext context, FlowChart chart)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		FlowChartModel model = (FlowChartModel) chart.getValue();
		writer.append(model.toJSON().toString());
	}

	protected void encodeOptions(FacesContext context, FlowChart chart)
			throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		writer.write("showGrid:" + chart.isShowGrid());
		writer.write(",closeGrid:" + chart.isCloseGrid());

		writer.write(",outerControl:" + chart.isEditable());

		writer.write(",offset:{");
		writer.write("x:" + chart.getOffsetX());
		writer.write(",y:" + chart.getOffsetY());
		writer.write("}");

		writer.write(",canvasConfig:{");
		writer.write("width:" + chart.getWidth());
		writer.write(",height:" + chart.getHeight());
		writer.write("}");

		writer.write(",scale:" + chart.getScale());
	}
}